package com.serv.common.route;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.serv.common.base.BaseController;
import com.serv.common.model.sys.User;
import com.serv.util.AjaxObject;
import com.serv.util.Mvcs;

public class AdminAuthInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation inv) {
		BaseController bc = (BaseController) inv.getController();
		
		User user = null;
		Object subject = bc.getSubject();
		if(subject instanceof User) user = (User) subject;
		
		if(user == null) {
			if(bc.isAjax()){
				bc.renderJson(AjaxObject.newTimeout("超时，请重新登录"));
			}else{
				String html = "<html><script>window.open ('"+bc.getRequest().getContextPath()+"/login','_top')</script></html>";
				bc.renderHtml(html);
			}
		}else{
			Mvcs.setSubject(user);
			inv.invoke();
			Mvcs.removeSubject();
		}
	}
}
